/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * mainGUI.java
 *
 * Created on Dec 3, 2009, 4:02:12 PM
 */

package client;

/**
 *
 * @author Andrew
 */
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Insets;
import common.*;
import java.awt.Color;

public class mainGUI extends javax.swing.JFrame {


Insets insets = new Insets(5,5,5,5);
sessionController s = new sessionController();

    //StyledDocument chat = new StyledDocument(sc);
    /** Creates new form mainGUI */
    public mainGUI() {
        initComponents();
        
                //lobbyArea.getStyledDocument();
        //addStylesToDocument(chat);
    }

    public void appendToLobby(Color c, String s) { // better implementation--uses StyleContext
        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
                                            StyleConstants.Foreground, c);

        int len = lobbyArea.getDocument().getLength(); // same value as getText().length();
        lobbyArea.setCaretPosition(len);  // place caret at the end (with no selection)
        lobbyArea.setCharacterAttributes(aset, false);
        lobbyArea.replaceSelection(s); // there is no selection, so inserts at caret
    }

    public String getChatSession() {
        return lobbyArea.getText();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        chatview = new javax.swing.JTabbedPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        consoleArea = new javax.swing.JTextArea();
        jScrollPane4 = new javax.swing.JScrollPane();
        lobbyArea = new javax.swing.JTextPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        txtEntry = new javax.swing.JTextField();
        btnSend = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        lblState = new javax.swing.JLabel();
        fileSend = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("QuickChat");

        chatview.setName("chatView"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        consoleArea.setColumns(20);
        consoleArea.setRows(5);
        consoleArea.setName("consoleArea"); // NOI18N
        consoleArea.setMargin(insets);

        consoleArea.setEditable(false);
        jScrollPane3.setViewportView(consoleArea);

        chatview.addTab("Console", jScrollPane3);

        jScrollPane4.setName("jScrollPane4"); // NOI18N

        lobbyArea.setName("lobbyArea"); // NOI18N
        jScrollPane4.setViewportView(lobbyArea);

        chatview.addTab("Lobby", jScrollPane4);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);

        jLabel1.setText("Online Users");
        jLabel1.setName("lblOnlineUsers"); // NOI18N

        txtEntry.setName("txtEntry"); // NOI18N
        txtEntry.setNextFocusableComponent(btnSend);

        btnSend.setText("Send");
        btnSend.setFocusCycleRoot(true);
        btnSend.setName("btnSend"); // NOI18N
        btnSend.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnSendMouseClicked(evt);
            }
        });
        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });
        btnSend.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnSendKeyPressed(evt);
            }
        });

        jLabel3.setText("Status:");
        jLabel3.setName("jLabel3"); // NOI18N

        lblState.setName("lblState"); // NOI18N

        fileSend.setText("Send File");
        fileSend.setName("fileSend"); // NOI18N
        fileSend.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                fileSendMouseClicked(evt);
            }
        });
        fileSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileSendActionPerformed(evt);
            }
        });

        jButton2.setText("New Tab");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        fileMenu.setText("File");

        jMenuItem1.setText("Settings");
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        fileMenu.add(jMenuItem1);

        jMenuItem3.setText("Save Chat Session");
        jMenuItem3.setName("jMenuItem3"); // NOI18N
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        fileMenu.add(jMenuItem3);

        jMenuItem2.setText("Disconnect");
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        jMenuItem2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenuItem2MouseClicked(evt);
            }
        });
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        fileMenu.add(jMenuItem2);

        menuBar.add(fileMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblState, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtEntry, javax.swing.GroupLayout.DEFAULT_SIZE, 577, Short.MAX_VALUE)
                            .addComponent(chatview, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 577, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnSend, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addComponent(fileSend, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(chatview, javax.swing.GroupLayout.PREFERRED_SIZE, 370, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(lblState, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addGap(8, 8, 8)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fileSend)
                        .addGap(95, 95, 95)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(btnSend, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtEntry, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSendMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnSendMouseClicked
        Thread myThread = new Thread()
       {
           public void run()
           {
               s.sendMessage();
           }
       };
       myThread.start();
    }//GEN-LAST:event_btnSendMouseClicked

    private void btnSendKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnSendKeyPressed
        final int charcode = evt.getKeyCode();
       Thread myThread = new Thread()
       {
           public void run()
           {
               if (charcode == 13)
               s.sendMessage();
           }
       };
       myThread.start();
    }//GEN-LAST:event_btnSendKeyPressed

    private void jMenuItem2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem2MouseClicked
        Thread myThread = new Thread()
       {
           public void run()
           {
               s.disconnect();
           }
       };
       myThread.start();
    }//GEN-LAST:event_jMenuItem2MouseClicked

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        Thread myThread = new Thread()
       {
           public void run()
           {
               s.disconnect();
           }
       };
       myThread.start();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void fileSendMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_fileSendMouseClicked
        Thread myThread = new Thread()
       {
           public void run()
           {

         Thread myThread = new Thread()
       {
           public void run()
           {
               int index = 0;
               String selectedUser = null;
               index = jList1.getSelectedIndex();


               if (index == 0)
               {
                   JFrame frame = new JFrame("Error");
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JOptionPane.showMessageDialog(frame,
                    "You cannot send yourself a file!",
                    "Not Available",
                    JOptionPane.INFORMATION_MESSAGE);
               } else
                   s.sendFile(selectedUser);

           }
       };
       myThread.start();
           }
       };
       myThread.start();
    }//GEN-LAST:event_fileSendMouseClicked

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        Thread myThread = new Thread()
       {
           public void run()
           {
               s.saveTextFile();
           }
       };
       myThread.start();
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        Thread myThread = new Thread()
       {
           public void run()
           {
               s.showSettings();
           }
       };
       myThread.start();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        Thread myThread = new Thread()
       {
           public void run()
           {
               int index = 0;
               String selectedUser = null;
               index =  jList1.getSelectedIndex();
               selectedUser = (String)jList1.getModel().getElementAt(index);
               if (index == 0)
               {
                   JFrame frame = new JFrame("Error");
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JOptionPane.showMessageDialog(frame,
                    "You cannot start a private chat with yourself",
                    "Not Available",
                    JOptionPane.INFORMATION_MESSAGE);
               } else
                   s.makeTab(selectedUser);

           }
       };
       myThread.start();
    }//GEN-LAST:event_jButton2MouseClicked

    private void fileSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileSendActionPerformed

    }//GEN-LAST:event_fileSendActionPerformed

    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnSendActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton btnSend;
    public javax.swing.JTabbedPane chatview;
    public javax.swing.JTextArea consoleArea;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JButton fileSend;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    public javax.swing.JList jList1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    public javax.swing.JLabel lblState;
    private javax.swing.JTextPane lobbyArea;
    private javax.swing.JMenuBar menuBar;
    public javax.swing.JTextField txtEntry;
    // End of variables declaration//GEN-END:variables

}
